package fileIO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: TODO
 * @author: wzj
 * @create: 2019/5/16 14:33
 */
public class NIODemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream("E:\\搜狗高速下载\\南屏晚钟.mp3");
        FileOutputStream fileOutputStream=new FileOutputStream("C:\\Users\\007\\Desktop\\南屏晚钟.mp3");
        long l=System.currentTimeMillis();

        /*//java io
        byte[] bytes=new byte[1024];
        int len;
        while((len=fileInputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,len);
        }
        fileOutputStream.close();
        fileInputStream.close();*/

        /*//java nio 获取通道
        FileChannel fileChannelIn=fileInputStream.getChannel();
        FileChannel fileChannelOut=fileOutputStream.getChannel();

        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        while(fileChannelIn.read(byteBuffer)!=-1){
            System.out.println("开始");
            System.out.println(byteBuffer.position());
            System.out.println(byteBuffer.limit());
            System.out.println(byteBuffer.capacity());
            byteBuffer.flip();
            System.out.println("flip");
            System.out.println(byteBuffer.position());
            System.out.println(byteBuffer.limit());
            System.out.println(byteBuffer.capacity());
            fileChannelOut.write(byteBuffer);
            System.out.println("write");
            System.out.println(byteBuffer.position());
            System.out.println(byteBuffer.limit());
            System.out.println(byteBuffer.capacity());
            byteBuffer.clear();
            System.out.println("clear");
            System.out.println(byteBuffer.position());
            System.out.println(byteBuffer.limit());
            System.out.println(byteBuffer.capacity());
        }
        fileChannelOut.close();
        fileChannelIn.close();
        fileOutputStream.close();
        fileInputStream.close();*/
        System.out.println("用时"+(System.currentTimeMillis()-l));
    }

    public static void Buffer(){
        ByteBuffer byteBuffer=ByteBuffer.allocate(10);

        String str="12345";
        System.out.println("最初");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        byteBuffer.put(str.getBytes());
        System.out.println("put 之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        byteBuffer.flip();
        System.out.println("flip 之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        System.out.println("读取 get "+byteBuffer.limit()+"个长度内容");
        byte[] dst=new byte[byteBuffer.limit()];
        byteBuffer.get(dst);
        System.out.println(new String(dst,0,dst.length));

        System.out.println("get 之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        byteBuffer.rewind();
        System.out.println("可重复读 之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        byteBuffer.clear();
        System.out.println("clear 之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("clear 之后,缓冲区是否还有内容,可操作的数量");
        System.out.println(byteBuffer.hasRemaining());
        System.out.println(byteBuffer.remaining());


        byteBuffer.mark();//标记position位置
        byteBuffer.put("aaa".getBytes());
        System.out.println("标记position位置,又存入aaa之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        byteBuffer.reset();
        System.out.println("reset 之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        byteBuffer.position(2);
        System.out.println("设置position为2 之后");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
    }
}
